<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="上一节介绍的是 I2C 每一位信号的时序流程，而 I2C 通信在字节级的传输中，也有固定的时序要求。I2C 通信的起始信号(Start)后，首先要发送一个从机的地址，这个地址一共有 7位，紧跟着的第 8 位是数据方向位(R/W)，“0”表示接下来要发送数据（写），‘“1”表示接下来是请求数据（读）。 我们知道，打电话的时候，当拨通电话，接听方捡起电话肯定要回一个“喂”，这就是告诉拨电话的人，这边有">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="14.2、I2C寻址模式">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu091/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="上一节介绍的是 I2C 每一位信号的时序流程，而 I2C 通信在字节级的传输中，也有固定的时序要求。I2C 通信的起始信号(Start)后，首先要发送一个从机的地址，这个地址一共有 7位，紧跟着的第 8 位是数据方向位(R/W)，“0”表示接下来要发送数据（写），‘“1”表示接下来是请求数据（读）。 我们知道，打电话的时候，当拨通电话，接听方捡起电话肯定要回一个“喂”，这就是告诉拨电话的人，这边有">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140320/1-140320225Q5254.png">
<meta property="og:updated_time" content="2019-01-28T13:27:06.129Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="14.2、I2C寻址模式">
<meta name="twitter:description" content="上一节介绍的是 I2C 每一位信号的时序流程，而 I2C 通信在字节级的传输中，也有固定的时序要求。I2C 通信的起始信号(Start)后，首先要发送一个从机的地址，这个地址一共有 7位，紧跟着的第 8 位是数据方向位(R/W)，“0”表示接下来要发送数据（写），‘“1”表示接下来是请求数据（读）。 我们知道，打电话的时候，当拨通电话，接听方捡起电话肯定要回一个“喂”，这就是告诉拨电话的人，这边有">
<meta name="twitter:image" content="http://c.biancheng.net/cpp/uploads/allimg/140320/1-140320225Q5254.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu091/">





  <title>14.2、I2C寻址模式 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu091/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">14.2、I2C寻址模式</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:37:14+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>上一节介绍的是 I2C 每一位信号的时序流程，而 I2C 通信在字节级的传输中，也有固定的时序要求。I2C 通信的起始信号(Start)后，首先要发送一个从机的地址，这个地址一共有 7位，紧跟着的第 8 位是数据方向位(R/W)，“0”表示接下来要发送数据（写），‘“1”表示接下来是请求数据（读）。</p>
<p>我们知道，打电话的时候，当拨通电话，接听方捡起电话肯定要回一个“喂”，这就是告诉拨电话的人，这边有人了。同理，这个第九位 ACK 实际上起到的就是这样一个作用。当我们发送完了这 7 位地址和 1 位方向后，如果发送的这个地址确实存在，那么这个地址的器件应该回应一个 ACK（拉低 SDA 即输出“0”），如果不存在，就没“人”回应 ACK（SDA将保持高电平即“1”）。</p>
<p>那我们写一个简单的程序，访问一下我们板子上的 EEPROM 的地址，另外再写一个不存在的地址，看看它们是否能回一个 ACK，来了解和确认一下这个问题。</p>
<p>我们板子上的 EEPROM 器件型号是 24C02，在 24C02 的数据手册 3.6 节中可查到，24C02的 7 位地址中，其中高 4 位是固定的 0b1010，而低 3 位的地址取决于具体电路的设计,由芯片上的 A2、A1、A0 这 3 个引脚的实际电平决定，来看一下我们的 24C02 的电路图，它和24C01 的原理图完全一样，如图 14-4 所示。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140320/1-140320225Q5254.png" alt></p>
<center><strong>图 14-4   24C02 原理图</strong></center>

<p>从图 14-4 可以看出来，我们的 A2、A1、A0 都是接的 GND，也就是说都是 0，因此 24C02的 7 位地址实际上是二进制的 0b1010000，也就是 0x50。我们用 I2C 的协议来寻址 0x50，另外再寻址一个不存在的地址 0x62，寻址完毕后，把返回的 ACK 显示到我们的 1602 液晶上，大家对比一下。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">/***************************Lcd1602.c 文件程序源代码*****************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">#define LCD1602_DB P0</span><br><span class="line">sbit LCD1602_RS = P1^0;</span><br><span class="line">sbit LCD1602_RW = P1^1;</span><br><span class="line">sbit LCD1602_E = P1^5;</span><br><span class="line">/* 等待液晶准备好 */</span><br><span class="line">void LcdWaitReady()&#123;</span><br><span class="line">    unsigned char sta;</span><br><span class="line">    LCD1602_DB = 0xFF;</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 1;</span><br><span class="line">    do &#123;</span><br><span class="line">        LCD1602_E = 1;</span><br><span class="line">        sta = LCD1602_DB; //读取状态字</span><br><span class="line">        LCD1602_E = 0;</span><br><span class="line">    &#125; while (sta &amp; 0x80); //bit7 等于 1 表示液晶正忙，重复检测直到其等于 0 为止</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节命令，cmd-待写入命令值 */</span><br><span class="line">void LcdWriteCmd(unsigned char cmd)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = cmd;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节数据，dat-待写入数据值 */</span><br><span class="line">void LcdWriteDat(unsigned char dat)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 1;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = dat;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 设置显示 RAM 起始地址，亦即光标位置，(x,y)-对应屏幕上的字符坐标 */</span><br><span class="line">void LcdSetCursor(unsigned char x, unsigned char y)&#123;</span><br><span class="line">    unsigned char addr;</span><br><span class="line">    if (y == 0)&#123; //由输入的屏幕坐标计算显示 RAM 的地址</span><br><span class="line">        addr = 0x00 + x; //第一行字符地址从 0x00 起始</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        addr = 0x40 + x; //第二行字符地址从 0x40 起始</span><br><span class="line">    &#125;</span><br><span class="line">    LcdWriteCmd(addr | 0x80); //设置 RAM 地址</span><br><span class="line">&#125;</span><br><span class="line">/* 在液晶上显示字符串，(x,y)-对应屏幕上的起始坐标，str-字符串指针 */</span><br><span class="line">void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)&#123;</span><br><span class="line">    LcdSetCursor(x, y);//设置起始地址</span><br><span class="line">    while (*str != &apos;\0&apos;)&#123; //连续写入字符串数据，直到检测到结束符</span><br><span class="line">        LcdWriteDat(*str++);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 初始化 1602 液晶 */</span><br><span class="line">void InitLcd1602()&#123;</span><br><span class="line">    LcdWriteCmd(0x38); //16*2 显示，5*7 点阵，8 位数据接口</span><br><span class="line">    LcdWriteCmd(0x0C); //显示器开，光标关闭</span><br><span class="line">    LcdWriteCmd(0x06); //文字不动，地址自动+1</span><br><span class="line">    LcdWriteCmd(0x01); //清屏</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br></pre></td><td class="code"><pre><span class="line">/*****************************main.c 文件程序源代码******************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">#include &lt;intrins.h&gt;</span><br><span class="line">#define I2CDelay() &#123;_nop_();_nop_();_nop_();_nop_();&#125;</span><br><span class="line">sbit I2C_SCL = P3^7;</span><br><span class="line">sbit I2C_SDA = P3^6;</span><br><span class="line">bit I2CAddressing(unsigned char addr);</span><br><span class="line">extern void InitLcd1602();</span><br><span class="line">extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);</span><br><span class="line">void main()&#123;</span><br><span class="line">    bit ack;</span><br><span class="line">    unsigned char str[10];</span><br><span class="line">    InitLcd1602(); //初始化液晶</span><br><span class="line">    ack = I2CAddressing(0x50); //查询地址为 0x50 的器件</span><br><span class="line">    str[0] = &apos;5&apos;; //将地址和应答值转换为字符串</span><br><span class="line">    str[1] = &apos;0&apos;;</span><br><span class="line">    str[2] = &apos;:&apos;;</span><br><span class="line">    str[3] = (unsigned char)ack + &apos;0&apos;;</span><br><span class="line">    str[4] = &apos;\0&apos;;</span><br><span class="line">    LcdShowStr(0, 0, str); //显示到液晶上</span><br><span class="line">    ack = I2CAddressing(0x62); //查询地址为 0x62 的器件</span><br><span class="line">    str[0] = &apos;6&apos;; //将地址和应答值转换为字符串</span><br><span class="line">    str[1] = &apos;2&apos;;</span><br><span class="line">    str[2] = &apos;:&apos;;</span><br><span class="line">    str[3] = (unsigned char)ack + &apos;0&apos;;</span><br><span class="line">    str[4] = &apos;\0&apos;;</span><br><span class="line">    LcdShowStr(8, 0, str); //显示到液晶上</span><br><span class="line">    while (1);</span><br><span class="line">&#125;</span><br><span class="line">/* 产生总线起始信号 */</span><br><span class="line">void I2CStart()&#123;</span><br><span class="line">    I2C_SDA = 1; //首先确保 SDA、SCL 都是高电平</span><br><span class="line">    I2C_SCL = 1;</span><br><span class="line">    I2CDelay();</span><br><span class="line">    I2C_SDA = 0; //先拉低 SDA</span><br><span class="line">    I2CDelay();</span><br><span class="line">    I2C_SCL = 0; //再拉低 SCL</span><br><span class="line">&#125;</span><br><span class="line">/* 产生总线停止信号 */</span><br><span class="line">void I2CStop()&#123;</span><br><span class="line">    I2C_SCL = 0; //首先确保 SDA、SCL 都是低电平</span><br><span class="line">    I2C_SDA = 0;</span><br><span class="line">    I2CDelay();</span><br><span class="line">    I2C_SCL = 1; //先拉高 SCL</span><br><span class="line">    I2CDelay();</span><br><span class="line">    I2C_SDA = 1; //再拉高 SDA</span><br><span class="line">    I2CDelay();</span><br><span class="line">&#125;</span><br><span class="line">/* I2C 总线写操作，dat-待写入字节，返回值-从机应答位的值 */</span><br><span class="line">bit I2CWrite(unsigned char dat)&#123;</span><br><span class="line">    bit ack; //用于暂存应答位的值</span><br><span class="line">    unsigned char mask; //用于探测字节内某一位值的掩码变量</span><br><span class="line">   </span><br><span class="line">    for (mask=0x80; mask!=0; mask&gt;&gt;=1)&#123; //从高位到低位依次进行</span><br><span class="line">        if ((mask&amp;dat) == 0)&#123; //该位的值输出到 SDA 上</span><br><span class="line">            I2C_SDA = 0;</span><br><span class="line">        &#125;else&#123;</span><br><span class="line">            I2C_SDA = 1;</span><br><span class="line">        &#125;</span><br><span class="line">        I2CDelay();</span><br><span class="line">    &#125;</span><br><span class="line">    I2C_SCL = 1; //拉高 SCL</span><br><span class="line">    I2CDelay();</span><br><span class="line">    I2C_SCL = 0; //再拉低 SCL，完成一个位周期</span><br><span class="line">    I2C_SDA = 1; //8 位数据发送完后，主机释放 SDA，以检测从机应答</span><br><span class="line">    I2CDelay();</span><br><span class="line">    I2C_SCL = 1; //拉高 SCL</span><br><span class="line">    ack = I2C_SDA; //读取此时的 SDA 值，即为从机的应答值</span><br><span class="line">    I2CDelay();</span><br><span class="line">    I2C_SCL = 0; //再拉低 SCL 完成应答位，并保持住总线</span><br><span class="line">    return ack; //返回从机应答值</span><br><span class="line">&#125;</span><br><span class="line">/* I2C 寻址函数，即检查地址为 addr 的器件是否存在，返回值-从器件应答值 */</span><br><span class="line">bit I2CAddressing(unsigned char addr)&#123;</span><br><span class="line">    bit ack;</span><br><span class="line">    I2CStart(); //产生起始位，即启动一次总线操作</span><br><span class="line">    //器件地址需左移一位，因寻址命令的最低位</span><br><span class="line">    //为读写位，用于表示之后的操作是读或写</span><br><span class="line">    ack = I2CWrite(addr&lt;&lt;1);</span><br><span class="line">    I2CStop(); //不需进行后续读写，而直接停止本次总线操作</span><br><span class="line">    return ack;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们把这个程序在 KST-51 开发板上运行完毕，会在液晶上边显示出来我们预想的结果，主机发送一个存在的从机地址，从机会回复一个应答位，即应答位为 0；主机如果发送一个不存在的从机地址，就没有从机应答，即应答位为 1。</p>
<p>前面的章节中已经提到利用库函数<em>nop</em>()可以进行精确延时，一个<em>nop</em>()的时间就是一个机器周期，这个库函数包含在 intrins.h 这个文件中，如果要使用这个库函数，只需要在程序最开始，和包含 reg52.h 一样，include&lt;intrins.h&gt;之后，程序中就可以使用这个库函数了。</p>
<p>还有一点要提一下，I2C 通信分为低速模式 100kbit/s、快速模式 400kbit/s 和高速模式3.4Mbit/s。因为所有的 I2C 器件都支持低速，但却未必支持另外两种速度，所以作为通用的I2C 程序我们选择 100k 这个速率来实现，也就是说实际程序产生的时序必须小于等于 100k的时序参数，很明显也就是要求 SCL 的高低电平持续时间都不短于 5us，因此我们在时序函数中通过插入 I2CDelay()这个总线延时函数（它实际上就是 4 个 NOP 指令，用 define 在文件开头做了定义），加上改变 SCL 值语句本身占用的至少一个周期，来达到这个速度限制。如果以后需要提高速度，那么只需要减小这里的总线延时时间即可。</p>
<p>此外我们要学习一个发送数据的技巧，就是 I2C 通信时如何将一个字节的数据发送出去。大家注意函数 I2CWrite 中，用的那个 for 循环的技巧。for (mask=0x80; mask!=0; mask&gt;&gt;=1)，由于 I2C 通信是从高位开始发送数据，所以我们先从最高位开始，0x80 和 dat 进行按位与运算，从而得知 dat 第 7 位是 0 还是 1，然后右移一位，也就是变成了用 0x40 和 dat 按位与运算，得到第 6 位是 0 还是 1，一直到第 0 位结束，最终通过 if 语句，把 dat 的 8 位数据依次发送了出去。其它的逻辑大家对照前边讲到的理论知识，认真研究明白就可以了。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu090/" rel="next" title="14.1、单片机I2C时序介绍">
                <i class="fa fa-chevron-left"></i> 14.1、单片机I2C时序介绍
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu092/" rel="prev" title="14.3、单片机EEPROM简介">
                14.3、单片机EEPROM简介 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
